package com.powerhua.core.base.dao;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.powerhua.core.base.bean.IField;
import com.powerhua.core.base.bean.IModel;

/**
 *
 * @author Brian.Xie
 */
public class CommonDao {

    private static final Log LOG = LogFactory.getLog(CommonDao.class);
    protected JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 
     * @param type
     * @param value
     * @return
     */
    protected Object convertType(String type, String value) {
        if ("timestamp".equals(type)) {
            return new Timestamp(Long.parseLong(value));
        } else if ("decimal".equals(type)) {
            return new BigDecimal(value);
        } else if ("varchar".equals(type)) {
            return value;
        } else if ("integer".equals(type) || "int".equals(type)) {
            return Integer.parseInt(value);
        } else if ("bigint".equals(type)) {
            return Long.parseLong(value);
        } else if ("float".equals(type)) {
            return Float.parseFloat(value);
        } else if ("double".equals(type)) {
            return Double.parseDouble(value);
        } else {
            LOG.error("Not support type");
            return null;
        }
    }

    /**
     * 
     * @param ps
     * @param number
     * @param type
     * @param value
     * @throws java.sql.SQLException
     */
    protected void setPreparedStatement(PreparedStatement ps,
            int number, String type, String value) throws SQLException {
        if ("timestamp".equals(type)) {
            ps.setTimestamp(number, new Timestamp(Long.parseLong(value)));
        } else if ("decimal".equals(type)) {
            ps.setBigDecimal(number, new BigDecimal(value));
        } else if ("varchar".equals(type)) {
            ps.setString(number, value);
        } else if ("integer".equals(type) || "int".equals(type)) {
            ps.setInt(number, Integer.parseInt(value));
        } else if ("bigint".equals(type)) {
            ps.setLong(number, Long.parseLong(value));
        } else if ("float".equals(type)) {
            ps.setFloat(number, Float.parseFloat(value));
        } else if ("double".equals(type)) {
            ps.setDouble(number, Double.parseDouble(value));
        } else {
            LOG.error("Not support type");
        }
    }

    /**
     * 
     * @param baseModelId
     * @return
     */
    protected boolean checkModel(IModel model) {
        // 从缓存中取出对应模型
        if (model == null) {
            LOG.error("No model found");
            return false;
        }

        // 取出模型主键
        IField pkField = model.getPk();
        if (pkField == null) {
            LOG.error("No primary key found");
            return false;
        }

        // 从模型中取出字段
        Map<String, IField> fields = model.getFields();
        if (fields == null || fields.size() == 0) {
            LOG.error("No table field found");
            return false;
        }

        return true;
    }
}
